#!/usr/bin/env python
# _*_coding:utf-8 _*_
__author__ = "Gao"
'''
时钟示例

'''
import sys, random, math, pygame
from pygame.locals import *
from datetime import datetime,date,time

def print_text(font,x,y,text,color=(255,255,255)):
    imgText = font.render(text,True,color)
    screen.blit(imgText,(x,y))


def wrap_angle(angle):    # 计算角度，避免出现负数
    return angle % 360

pygame.init()

screen = pygame.display.set_mode((600,500))

font = pygame.font.Font(None,36)
orange = 220,180,0
white = 255,255,255
yellow = 255,255,0
pink = 255,100,100

pos_x = 300
pos_y = 250
radius = 250
angle = 360

while True:
    for event in pygame.event.get():
        if event.type == QUIT:
            sys.exit()
        keys = pygame.key.get_pressed()
        if keys[K_ESCAPE]:
            sys.exit()
    screen.fill((0,0,100))    # 每循环一次，清空整个画面

    pygame.draw.circle(screen,white,(pos_x,pos_y),radius,6)    # 时钟圆周

    # 画1-12
    for n in range(1,13):
        angle = math.radians(n*(360/12)-90)    # 计算每个数字所在的弧度，由于12点是在负90度的位置，因此每个数字的角度要减90
        x = math.cos(angle) * (radius-20)-10
        y = math.sin(angle) * (radius-20)-10
        print_text(font,pos_x+x,pos_y+y,str(n))

    today = datetime.today()
    hours = today.hour % 12    # 计算当前小时(12小时制)
    minutes = today.minute    # 当前分钟
    seconds = today.second    #当前秒

    hour_angle = wrap_angle(hours * (360/12)-90)    # 当前小时所在的角度
    hour_angle = math.radians(hour_angle)    # 当前小时所在的弧度
    hour_x = math.cos(hour_angle)*(radius-80)
    hour_y = math.sin(hour_angle)*(radius-80)
    hour_target = (pos_x+hour_x,pos_y+hour_y)    # 时针
    pygame.draw.line(screen,pink,(pos_x,pos_y),hour_target,25)    #画时针

    minute_angle = wrap_angle(minutes*(360/60)-90)
    minute_angle = math.radians(minute_angle)
    minute_x = math.cos(minute_angle)*(radius-60)
    minute_y = math.sin(minute_angle)*(radius-60)
    minute_target = (pos_x + minute_x, pos_y + minute_y)  # 分针
    pygame.draw.line(screen, orange, (pos_x, pos_y), minute_target, 12)  # 画分针

    second_angle = wrap_angle(seconds*(360/60)-90)
    second_angle = math.radians(second_angle)
    second_x = math.cos(second_angle)*(radius-40)
    second_y = math.sin(second_angle)*(radius-40)
    second_target = (pos_x + second_x, pos_y + second_y)  # 秒针
    pygame.draw.line(screen, yellow, (pos_x, pos_y), second_target, 6)  # 画m秒针

    pygame.draw.circle(screen,white,(pos_x,pos_y),20,0)    # 覆盖时钟中心
    print_text(font,0,0,str(hours) + ":"+str(minutes)+":"+str(seconds))
    pygame.display.update()